package com.sp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sp.model.entity.Menu;
import com.sp.model.entity.Result;
import com.sp.model.vo.MenuVO;
import com.sp.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.sp.constant.MenuMessageConstant.SELECT_MENU_SUCCESS;

/**
 * @author tong
 * @date 2024/4/15 10:05
 */
@Api(tags = "界面菜单管理api接口")
@RestController
@RequestMapping("/menus")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("根据权限角色id查询界面菜单")
    @GetMapping("/{roleId}")
    public Result<List<MenuVO>> getMenus(
            @ApiParam(name = "roleId", value = "权限角色id") @PathVariable("roleId") List<Integer> roleId) {
        List<Menu> menus = menuService.getMenusByRoleIds(roleId);

        return Result.success(SELECT_MENU_SUCCESS, BeanUtil.copyToList(menus, MenuVO.class));
    }

    @ApiOperation("查询所有界面菜单")
    @GetMapping("/selectAll")
    public Result<List<MenuVO>> selectAll() {
        List<Menu> menus = menuService.selectAll();
        if (menus == null || menus.size() == 0) {
            return Result.success("具体权限数据查询完毕，但数据为空，请联系管理员处理");
        }

        return Result.success(SELECT_MENU_SUCCESS, BeanUtil.copyToList(menus, MenuVO.class));
    }

}
